home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / gopher / Unix / xgopher.1.3 / subst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  3.4 KB  |  144 lines

  1. /* subst.c
  2.    perform editing substitutions on commands that Xgopher will execute */
  3.  
  4.      /*---------------------------------------------------------------*/
  5.      /* Xgopher        version 1.3     08 April 1993                  */
  6.      /*                version 1.2     20 November 1992               */
  7.      /*                version 1.1     20 April 1992                  */
  8.      /*                version 1.0     04 March 1992                  */
  9.      /* X window system client for the University of Minnesota        */
  10.      /*                                Internet Gopher System.        */
  11.      /* Allan Tuchman, University of Illinois at Urbana-Champaign     */
  12.      /*                Computing and Communications Services Office   */
  13.      /* Copyright 1992, 1993 by                                       */
  14.      /*           the Board of Trustees of the University of Illinois */
  15.      /* Permission is granted to freely copy and redistribute this    */
  16.      /* software with the copyright notice intact.                    */
  17.      /*---------------------------------------------------------------*/
  18.  
  19. #include <stdio.h>
  20. #include "conf.h"
  21. #include "gopher.h"
  22.  
  23. #include "osdep.h"
  24.  
  25. #define    PERCENT        '%'
  26.  
  27. #define N_EDIT        7
  28.  
  29. char        keys[N_EDIT] = "hpnsfP";
  30. static char    *editString();
  31.  
  32. /* editCommand
  33.    edit item information into a command used for processing a gopher item.
  34.    The return value is dynamic, so caller should free it. */
  35.  
  36. char    *
  37. editCommand(gi, command, fileName, suffix)
  38. gopherItemP    gi;
  39. char        *command;
  40. char        *fileName;
  41. char        *suffix;
  42. {
  43.     char    *editedCommand;
  44.     char    *values[N_EDIT];
  45.     char    portStr[8], altPortStr[8];
  46.  
  47.     if (gi != (gopherItemP) NULL) {
  48.         sprintf(portStr, "%6d", gi->port);
  49.         if (gi->port == 0  || gi->port == 23) {
  50.             strcpy(altPortStr, " ");
  51.         } else {
  52.             strcpy(altPortStr, portStr);
  53.         }
  54.  
  55.         values[0] = gi->host;
  56.         values[1] = portStr;
  57.         values[2] = USER_STRING(gi);
  58.         values[3] = vStringValue(&(gi->selector));
  59.         values[4] = (fileName == (char *) NULL) ? "" : fileName;
  60.         values[5] = altPortStr;
  61.     } else {
  62.         values[0] = "";
  63.         values[1] = "";
  64.         values[2] = "";
  65.         values[3] = "";
  66.         values[4] = (fileName == (char *) NULL) ? "" : fileName;
  67.         values[5] = "";
  68.     }
  69.  
  70. #ifdef DEBUG
  71.     fprintf (stderr, "Edit the command: \'%s\'\n", command);
  72. #endif    /* DEBUG */
  73.  
  74.     editedCommand =
  75.         editString(command, keys, values, N_EDIT, suffix);
  76.  
  77. #ifdef DEBUG
  78.     fprintf (stderr, "Edited command: \'%s\'\n", editedCommand);
  79. #endif    /* DEBUG */
  80.  
  81.     return editedCommand;
  82. }
  83.  
  84.  
  85. /* editString
  86.    edit printf-like strings to replace escape sequences by new values.
  87.    The return value is dynamic, so caller should free it. */
  88.  
  89. static char    *
  90. editString(in, key, value, n, suffix)
  91. char    *in;
  92. char    key[];
  93. char    *value[];
  94. char    *suffix;
  95. int    n;
  96. {
  97.     char    *cmd = malloc(sizeof(char) * PATH_NAME_LEN);
  98.     char    *out, *c, *f;
  99.     char    **v;
  100.     BOOLEAN    edited = FALSE;
  101.  
  102.     out = cmd;
  103.  
  104.     while(*in != NULLC) {
  105.         if (*in != PERCENT) {
  106.             *(out++) = *(in++);
  107.         } else {
  108.             if (++in == NULLC) {
  109.                 *(out++) = PERCENT;
  110.                 break;
  111.             }
  112.  
  113.             for (c=key, v=value; (*c != NULLC && *c != *in);
  114.                                 c++, v++) ;
  115.  
  116.             if (*c != NULLC) {
  117.                 f = *v;
  118.                 while (*f != NULLC) {
  119.                     *(out++) = *(f++);
  120.                 }
  121.                 edited = TRUE;
  122.             } else {
  123.                 *(out++) = PERCENT;
  124.                 if (*in != PERCENT) {
  125.                     *(out++) = *(in++);
  126.                 }
  127.             }
  128.             ++in;
  129.         }
  130.     }
  131.  
  132.     if (! edited  &&  suffix != (char *) NULL) {
  133.         *(out++) = ' ';
  134.         f = suffix;
  135.         while (*f != NULLC) {
  136.             *(out++) = *(f++);
  137.         }
  138.     }
  139.  
  140.     *(out++) = '\0';
  141.     
  142.     return cmd;;
  143. }
  144.